﻿CKEDITOR.dialog.add( 'autoform', function( editor ) {
		function addOption(combo, optionText, optionValue, documentObject, index ) {
		     var oOption = documentObject.createElement( "OPTION" );
		     if ( CKEDITOR.env.ie ) {
		    	 if(!isNaN( parseInt( index, 10))){
		    		 combo.$.options.add( oOption.$, index );				 	
				 }else{
		    		 combo.$.options.add( oOption.$ );				 	
				 }
		    	 
		    	 oOption.$.innerHTML = optionText.length > 0 ? optionText : '';
		    	 oOption.$.value = optionValue;
		     } else {
		    	 if ( index !== null && index < combo.getChildCount() ){
		    		 combo.getChild( index < 0 ? 0 : index ).insertBeforeMe( oOption );		    		 
		    	 } else {
		    		 combo.append( oOption );		    		 
		    	 }
		    	 oOption.setText( optionText.length > 0 ? optionText : '' );
		    	 oOption.setValue( optionValue );
		     }
		     return oOption;
		};
        return {
                title: "自动表单信息编辑",
                minWidth: 260,
                minHeight: 260,
				onOk:function(){
					var data={};
					this.commitContent(data);
					var editor = this.getParentEditor();
					var table = new CKEDITOR.dom.element('table');
					table.setAttribute("border","1");
					table.setAttribute("cellpadding","5");
					table.setAttribute("cellspacing","2");
					table.setStyle("border","solid 1px #F5F5F5");
					table.setStyle("width","100%");
					table.setStyle("border-collapse","collapse");
					table.setStyle("font-size","12px");
					if(!data.fields){
						alert("请选择要添加的字段信息");
						return;
					}
					var cols=data.cols;
					if(!cols)cols=1;
					cols=parseInt(cols);
					var fields=data.fields;
					var length=fields.length;
					for(var i=0;i<length;i++){
						var tr=new CKEDITOR.dom.element('tr');
						for(var j=0;j<cols;j++){
							var field=fields[i];
							var text=field.text;
							var value=field.value;
							var tdName=new CKEDITOR.dom.element('td');
							tdName.setText(text+":");
							tdName.setStyle("text-align","right");
							tdName.setStyle("width","90px");
							tr.append(tdName);						
							var tdValue=new CKEDITOR.dom.element('td');
							var div=new CKEDITOR.dom.element('div');
							div.setAttribute("class","uflotext");
							div.setAttribute("type","text");
							div.setAttribute("bindingfield",value);
							div.setAttribute("id",CKEDITOR.tools.getNextId());
							div.setStyle("width","100%");
							div.setText("$F{"+value+"}");
							tdValue.append(div);
							tr.append(tdValue);
							if(j<cols-1){
								i++;
							}
							if(i==length){
								i--;
								break;
							}
						}
						if(i==length-1 && length%cols>0){
							var size=cols-length%cols;
							var td=new CKEDITOR.dom.element('td');
							td.appendHtml("&nbsp;");
							td.setAttribute("colspan",size*2);
							tr.append(td)
						}
						table.append(tr);
					}
					editor.insertElement(table);
				},
                contents: [
                        {
                        id: 'info',
                        elements: [
                                   {
                                	   type: 'hbox',
                   						widths: ['65%','35%'],
                   						children:[
	                   						          {
	                   						        	  id: 'columnlist',
	                   						        	  type: 'select',
	                   						        	  style:"width:200px",
	                   						        	  label: "选择要添加的字段信息",
	                   						        	  'default': '',
	                   						        	  items:window.formTable.getMasterColumns()
	                   						          },
	                   						          {
	                   						        	  type:"button",
	                   						        	  style:"margin-top:14px",
	                   						        	  label:"添加",
	                   						        	  onClick:function(){
	                   						        		var dialog = this.getDialog(),
	                   						        			parentEditor = dialog.getParentEditor(),
	                   						        			currentButton = dialog.getContentElement('info', 'columnlist' );
	                   						        		var dom=currentButton.getInputElement().$;
	                   						        		var selectedIndex=dom.selectedIndex;
	                   						        		if(selectedIndex==-1){
	                   						        			alert("请先选择一个要添加的字段信息!");
	                   						        			return;
	                   						        		}
	                   						        		var addedcolumns=dialog.getContentElement('info', 'addedcolumns' );
	                   						        		var text=dom.options[selectedIndex].text;
	                   						        		var value=dom.options[selectedIndex].value;
	                   						        		var selectedOptions=addedcolumns.getInputElement().$.options;
	                   						        		for(var i=0;i<selectedOptions.length;i++){
	                   						        			var option=selectedOptions[i];
	                   						        			if(option.value==value && option.text==text){
	                   						        				alert("当前选择的字段信息已存在!");
	                   						        				return;
	                   						        			}
	                   						        		}
	                   						        		addedcolumns.add(text,value);
	                   						        	  }
	                   						          }
                   						 ]
                                   },
                                   {
                                	   type:"hbox",
                                	   children:[
                                	             {
                                	            	 id:"addedcolumns",
                                	            	 type:"select",
                                	            	 label: "已添加的字段信息",
                                                     size: 5,
                                                     style: 'width:200px;height:130px',
                                                     items:[],
                                                     setup:function(widget){
														this.clear();
                                                    	var elements=widget.element.getChildren();
                                                    	if(!elements)return;
                                                    	for(var i=0;i<elements.count();i++){
                                                    		var element=elements.getItem(i);
                                                    		if(!element.hasClass("uflogridheader")){
																continue;
															}
															var nodes=element.getChildren();
															for(var j=0;j<nodes.count();j++){
																var node=nodes.getItem(j);
																if(!node.hasClass("uflogridcolumn")){
																	continue;
																}
																this.add(node.getText(),node.getAttribute("bindingfield"));
															}
                                                    	}
                                                     },
                                                     commit:function(data){
                                                    	 var columns=[];
                                                    	 var options=this.getInputElement().$.options;
                                                    	 for(var i=0;i<options.length;i++){
                                                    		 var option=options[i];
                                                    		 columns.push({text:option.text,value:option.value});
                                                    	 }
                                                    	 data.fields=columns;
                                                     }
                                	             },
                                	             {
                                	            	 type:"vbox",
                                	            	 children:[
                                	            	           {
                                	            	        	 type:"button",
                                              	            	 label:"删除",
                                              	            	 style:"width:100%;margin-top:10px",
                                              	            	 onClick:function(){
                                              	            		var dialog = this.getDialog(),
                                              	            			addedcolumns=dialog.getContentElement('info', 'addedcolumns');
                                              	            		var element=addedcolumns.getInputElement();
        	                   						        		var selectedIndex=element.$.selectedIndex;
        	                   						        		if(selectedIndex==-1){
        	                   						        			alert("请先选择要删除的字段信息");
        	                   						        			return;
        	                   						        		}
        	                   						        		for ( var i = element.getChildren().count() - 1; i >= 0; i-- ) {
        	                   					                        if ( element.getChild( i ).$.selected ){
        	                   					                        	element.getChild( i ).remove();        	                   					                        	
        	                   					                        }
        	                   						        		}
                                              	            	 }
                                	            	           },
                                	            	           {
                                	            	        	   type:"button",
                                	            	        	   label:"上移",
                                	            	        	   onClick:function(){
                                	            	        		   var dialog = this.getDialog(),
                                             	            				addedcolumns=dialog.getContentElement('info', 'addedcolumns'),
                                             	            				element=addedcolumns.getInputElement();
                                	            	        		   var selectedIndex=element.$.selectedIndex;
	           	                   						        		if(selectedIndex==-1){
	           	                   						        			alert("请先选择要上移的字段信息");
	           	                   						        			return;
	           	                   						        		}
	           	                   						        		if(selectedIndex==0){
	           	                   						        			alert("当前列信息已处于顶部，不能再上移！");
	           	                   						        			return;
	           	                   						        		}
	           	                   						        		var text=element.$.options[selectedIndex].text;
	           	                   						        		var value=element.$.options[selectedIndex].value;
		           	                   						        	for (var i = element.getChildren().count() - 1; i >= 0; i-- ) {
	        	                   					                        if ( element.getChild( i ).$.selected ){
	        	                   					                        	element.getChild( i ).remove();        	                   					                        	
	        	                   					                        }
	        	                   						        		}
		           	                   						        	addOption(element,text,value,editor.document,selectedIndex-1);
                                	            	        	   }
                                	            	           },
                                	            	           {
                                	            	        	   type:"button",
                                	            	        	   label:"下移",
                                	            	        	   onClick:function(){
                                	            	        		   var dialog = this.getDialog(),
                                             	            				addedcolumns=dialog.getContentElement('info', 'addedcolumns'),
                                             	            				element=addedcolumns.getInputElement();
                                	            	        		   var selectedIndex=element.$.selectedIndex;
                                	            	        		   var options=element.$.options;
	           	                   						        		if(selectedIndex==-1){
	           	                   						        			alert("请先选择要下移的字段信息");
	           	                   						        			return;
	           	                   						        		}
	           	                   						        		if(selectedIndex==options.length-1){
	           	                   						        			alert("当前列信息已处于底部，不能再下移！");
	           	                   						        			return;
	           	                   						        		}
	           	                   						        		var text=options[selectedIndex].text;
	           	                   						        		var value=options[selectedIndex].value;
		           	                   						        	for (var i = element.getChildren().count() - 1; i >= 0; i-- ) {
	        	                   					                        if ( element.getChild( i ).$.selected ){
	        	                   					                        	element.getChild( i ).remove();        	                   					                        	
	        	                   					                        }
	        	                   						        		}
		           	                   						        	addOption(element,text,value,editor.document,selectedIndex+1);
                                	            	        	   }
                                	            	           }
                                	            	 ]
                                	             }
                                	   ]
                                   },
									{
										id:"columns",
										type:"text",
										label:"<br>列数：",
										//validate: CKEDITOR.dialog.validate.integer(editor.lang.common.validateNumberFailed),
										style: 'width:200px',
										setup:function(widget){
											this.setValue(2);
										},
					                    commit:function(data){
											data.cols=this.getValue();
										}  
									}
                        ]
                }
                ]
        };
});